/*
 * 辛克设定
 * 舞者无罪
 * 2014.12.04
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhedit.h"
#include "mhlabel.h"
#include "sink.h"
#include "com.h"
#include "keypane.h"
#include "mh/common.h"
#include "info.h"
#include "sink_setting.h"

#define SINK_CAPTION_LEFT     (CX_EDGE_LEFT)
#define SINK_CAPTION_TOP      (CY_EDGE_TOP + CX_EDGE_LEFT)
#define SINK_CAPTION_WIDTH    (80)
#define SINK_CAPTION_HEIGHT   (24)
#define SINK_CAPTION_INTER    (CX_SINK_BMP - CX_EDGE_LEFT * 2) / 9
#define SINK_CAPTION_TOP_DLTA   (SINK_CAPTION_TOP + 10)
#define SINK_EDIT_ZERO   (90)
/*
 *
 */
static void CreateSinkInterface(HWND hWnd)
{
    char sc[5] = {"0"};
    CreateWindow (MH_STATIC_NAME,
    "",
    WS_CHILD | WS_VISIBLE,
    ID_ESC,
    CX_SINK_BMP - CX_ASK_BMP-20,
    10,
    CX_ASK_BMP,
    CY_ASK_BMP,
    hWnd, (int)&g_bmp[BMP_FALSE]);

   CreateWindow(MH_LABEL_NAME,
        MW_LEFT_DRT_CAPTION,
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        SINK_CAPTION_LEFT + SINK_CAPTION_INTER * 2,
        SINK_CAPTION_TOP,
        SINK_CAPTION_WIDTH + SINK_CAPTION_INTER,
        SINK_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow(MH_LABEL_NAME,
        MW_RIGHT_DRT_CAPTION,
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        SINK_CAPTION_LEFT + SINK_CAPTION_INTER * 6,
        SINK_CAPTION_TOP,
        SINK_CAPTION_WIDTH + SINK_CAPTION_INTER,
        SINK_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow(MH_LABEL_NAME,
        MW_SINK_FRONT_ZERO,
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        SINK_CAPTION_LEFT + SINK_CAPTION_INTER ,
        SINK_CAPTION_TOP_DLTA + SINK_CAPTION_HEIGHT* 12,
        SINK_CAPTION_WIDTH,
        SINK_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetFrontSinkZero(g_knit_data));
    CreateWindow(MH_EDIT_NAME,
        sc,
        WS_CHILD | BS_CENTER | WS_VISIBLE,
        IDC_EDIT1 + SINK_EDIT_ZERO,
        SINK_CAPTION_LEFT + SINK_CAPTION_INTER * 2,
        SINK_CAPTION_TOP_DLTA + SINK_CAPTION_HEIGHT* 12,
        SINK_CAPTION_WIDTH,
        SINK_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow(MH_LABEL_NAME,
        MW_SINK_BACK_ZERO,
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        SINK_CAPTION_LEFT + SINK_CAPTION_INTER * 3,
        SINK_CAPTION_TOP_DLTA + SINK_CAPTION_HEIGHT* 12,
        SINK_CAPTION_WIDTH,
        SINK_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetBackSinkZero(g_knit_data));
    CreateWindow(MH_EDIT_NAME,
        sc,
        WS_CHILD | BS_CENTER | WS_VISIBLE,
        IDC_EDIT1 +SINK_EDIT_ZERO + 1,
        SINK_CAPTION_LEFT + SINK_CAPTION_INTER * 4,
        SINK_CAPTION_TOP_DLTA + SINK_CAPTION_HEIGHT* 12,
        SINK_CAPTION_WIDTH,
        SINK_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_BTN_NAME,
        MW_SINK_RESET,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_SINK_RESET,
        SINK_CAPTION_LEFT + SINK_CAPTION_INTER * 6 ,
        SINK_CAPTION_TOP_DLTA + 12 * SINK_CAPTION_HEIGHT,
        SINK_CAPTION_WIDTH,
        SINK_CAPTION_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    CreateWindow (MH_BTN_NAME,
        MW_SINK_COPY,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_SINK_COPY,
        SINK_CAPTION_LEFT + SINK_CAPTION_INTER * 7,
        SINK_CAPTION_TOP_DLTA + 12 * SINK_CAPTION_HEIGHT,
        SINK_CAPTION_WIDTH,
        SINK_CAPTION_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);

    int i = 0, j = 0;
    for(j = 0; j < 10; ++j)
    {
        char str[32] = {0};
        sprintf(str, "#%02d", j + 1);
        CreateWindow(MH_LABEL_NAME,
            str,
            WS_CHILD | WS_VISIBLE,
            IDC_STATIC,
            SINK_CAPTION_LEFT,
            SINK_CAPTION_TOP + (j + 2) * SINK_CAPTION_HEIGHT,
            SINK_CAPTION_WIDTH,
            SINK_CAPTION_HEIGHT,
            hWnd, MH_LABEL_COLOR_1GROUP);
    }
    for(i = 0; i < 8; ++i)
    {
        if(1)
        {
            const char *str[4] = {
                MW_FRONT_KNIT_CAPTION,
                MW_BACK_KNIT_CAPTION,
                MW_FRONT_TRAN_CAPTION,
                MW_BACK_TRAN_CAPTION
            };
            CreateWindow(MH_LABEL_NAME,
                str[i & 3],
                WS_CHILD | WS_VISIBLE,
                IDC_STATIC,
                SINK_CAPTION_LEFT + SINK_CAPTION_INTER * (i + 1),
                SINK_CAPTION_TOP + SINK_CAPTION_HEIGHT,
                SINK_CAPTION_WIDTH,
                SINK_CAPTION_HEIGHT,
                hWnd, MH_LABEL_COLOR_1GROUP);
        }
        char sc[32] = {0};
        for(j = 0; j < 10; ++j)
        {
            sprintf(sc, "%u", GetRunSink(g_knit_data, j, i));
            CreateWindow(MH_EDIT_NAME,
                sc,
                WS_CHILD | BS_CENTER | WS_VISIBLE,
                IDC_EDIT1 + i * 10 + j,
                SINK_CAPTION_LEFT + SINK_CAPTION_INTER * (i + 1),
                SINK_CAPTION_TOP + (j + 2) * SINK_CAPTION_HEIGHT,
                SINK_CAPTION_WIDTH,
                SINK_CAPTION_HEIGHT,
                hWnd, 0);
        }
    }
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    ushort n = atoi(sc);
    int sect = (nID - IDC_EDIT1) / 10;
    int which = (nID - IDC_EDIT1) % 10;

    if(nID - IDC_EDIT1 < SINK_EDIT_ZERO)
    {
        SetSinkFocus(g_knit_data, nID);
        SetRunSink(g_knit_data, which, sect, n);
        SetWindowAdditionalData(hWnd, TRUE);
         if(nID - IDC_EDIT1 >= 40)
        {
            n = -n;
        }
        SinkType sink_type = (SinkType)(((nID - IDC_EDIT1) / 10) % 2);
        sink_setting_exec(GetSinkSettingData(g_knit_data), LEFT_CARRIAGE, sink_type, n);
    }
    else
    {
        short fzero = 0;
        short bzero = 0;
        nID == IDC_EDIT1 +SINK_EDIT_ZERO ? SetFrontSinkZero(g_knit_data, n) : SetBackSinkZero(g_knit_data, n);
        SetWindowAdditionalData(hWnd, TRUE);

        fzero = GetFrontSinkZero(g_knit_data);
        bzero = GetBackSinkZero(g_knit_data);
        sink_setting_set_zero(GetSinkSettingData(g_knit_data), fzero, bzero);
    }
    data.sink.change = TRUE;
    SetWindowAdditionalData(hWnd, data.value);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(nID >= IDC_EDIT1 && nID <= IDC_EDIT1 + SINK_EDIT_ZERO + 1)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        RECT rect;
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        CreateKeyboardWindow(hWnd, rect, 0, 650, atoi(GetWindowCaption(hTmp)));
    }
    else if(nID == ID_SINK_RESET)
    {
        int focus = GetSinkFocus(g_knit_data);
        SetFocusChild(GetDlgItem(hWnd, focus));
        sink_setting_reset(GetSinkSettingData(g_knit_data), LEFT_CARRIAGE);
    }
    else if(nID == ID_SINK_COPY)
    {
        int focus = GetSinkFocus(g_knit_data);
        int index = focus - IDC_EDIT1;
        int sect = index / 10;
        int which = index % 10;
        int i = 0;
        SetFocusChild(GetDlgItem(hWnd, focus));
        HWND hTmd = GetDlgItem(hWnd, focus);
        const char *sc = GetWindowCaption(hTmd);
        for(i = 0; i < 10 - which; i++)
        {
            hTmd = GetDlgItem(hWnd, focus + i);
            SetRunSink(g_knit_data, which + i, sect, atoi(sc));
            SetWindowCaption(hTmd, sc);
            InvalidateRect(hTmd, NULL, TRUE);

        }
    }
    else if(nID == ID_ESC)
    {
         int ret = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
        if(ret)
        {
            printf("write param: %d\n", ret);
            AfxInfoBox(hWnd, NULL, GetString(481));
        }
        else
        {
            MH_FILE_SYNC();
        }

        SINK_SETTING *sink_setting = GetSinkSettingData(g_knit_data);
        if (sink_setting != NULL)
        {
            sink_setting_close(sink_setting);
            sink_setting = NULL;
        }
        if(data.sink.change)
        {
            SendSinkParam(g_knit_data);
            data.sink.change = FALSE;
        }
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
    }
    SetWindowAdditionalData(hWnd, data.value);
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 70)
        {
            nID += 10;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 + 10 && nID < IDC_EDIT1 + 80)
        {
            nID -= 10;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 80)
        {
            if((nID - IDC_EDIT1) % 10)
            {
                --nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 80)
        {
            if(((nID - IDC_EDIT1) % 10) < 9)
            {
                ++nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
}
/*
 *
 */
static int SinkWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            short fzero = 0;
            short bzero = 0;
            CreateSinkInterface(hWnd);
            SinkSettingInit(g_knit_data);
            sink_setting_set_zero(GetSinkSettingData(g_knit_data), fzero, bzero);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hosting = GetHosting(hWnd);
            if(hosting)
            {
                EnableWindow(hosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup (hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
          //  sink_setting_close(GetSinkSettingData(g_knit_data));
        }
        break;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_SINK]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_SETDATA:
        OnSetKnitValue(hWnd, LOWORD(wParam));
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
*
*/
HWND CreateSinkWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_SINK_BMP) / 2;
    int y = (CY_SCREEN - CY_SINK_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_SINK_SET_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = SinkWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_SINK_BMP;
    CreateInfo.by = y + CY_SINK_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
